#include "hplczkyspumpparsesystempacket.h"
#include "common/hplczkyspfcid.h"
#include "common/hplczkyspacketformat.h"
#include <QDebug>

#include "modulehplccmdid.h"

namespace hplc
{
namespace zkys
{

HplcZkysPumpParseSystemPacket::HplcZkysPumpParseSystemPacket(HplcZkysPumpProtocolData &data, hplc::PumpParam &param, QObject *parent)
    : QObject (parent)
    , m_d (&data)
    , m_param(&param)
{

}

void HplcZkysPumpParseSystemPacket::parseSet(quint8 pfc, char *data, int len)
{
    switch (pfc)
    {
    case PFC_PES_WORKMODE:
    {
        m_param->systemParam.workMode = data[0];

        QVariant var;
        var.setValue(m_param->systemParam.workMode);
        emit parseCompleted(hplc::PumpSystemCmdID_SetWorkMode, var);
    }
        break;
    case PFC_PES_FLOWCALIB_DATA_CMD:
        if (data[0])
        {
//            qDebug() << "clear ratecalibtable";
            m_param->systemParam.rateCalibTable.clear();
        }
        else
        {
            // 保存

//            qDebug() << "ratecalibratetable = ";
//            foreach (auto var, m_param->systemParam.rateCalibTable)
//            {
//                qDebug() << var.rpm << var.rate;
//            }

            QVariant var;
            var.setValue(m_param->systemParam.rateCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetFlowCalibData, var);
        }
        break;
    case PFC_PES_FLOWCALIB_DATA:
    {
        hplc::PumpRateCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.rpm = *(float*)(&temp);

        if (m_param->systemParam.rateCalibTable.size())
        {
            if (qFuzzyCompare(cell.rpm, m_param->systemParam.rateCalibTable.last().rpm))
                break;
        }

        temp = PTR2U32(data + 4);
        cell.rate = *(float*)(&temp);
        m_param->systemParam.rateCalibTable << cell;
    }
        break;
    case PFC_PES_FLOWCALIB_LOADPARAM:
    {
        quint32 temp = PTR2U32(data);
        float flow = *(float*)(&temp);
        m_param->systemParam.loadCalibParam.rate = flow;

        temp = PTR2U32(data + 4);
        flow = *(float*)(&temp);
        m_param->systemParam.loadCalibParam.realRate = flow;

        temp = PTR2U32(data + 8);
        flow = *(float*)(&temp);
        m_param->systemParam.loadCalibParam.pressure = flow;

        QVariant var;
        var.setValue(m_param->systemParam.loadCalibParam);
        emit parseCompleted(hplc::PumpSystemCmdID_SetFlowCalibLoadParam, var);
    }
        break;
    case PFC_PES_PRESSCALIB_DATA_CMD:
        if (data[0])
        {
            m_param->systemParam.pressureCalibTable.clear();
        }
        else
        {
            // 保存

            QVariant var;
            var.setValue(m_param->systemParam.pressureCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetPressCalibData, var);
        }
        break;
    case PFC_PES_PRESSCALIB_DATA:
    {
        hplc::PumpPressureCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.adc = *(float*)(&temp);

        if (m_param->systemParam.pressureCalibTable.size())
        {
            if (qFuzzyCompare(cell.adc, m_param->systemParam.pressureCalibTable.last().adc))
                break;
        }

        temp = PTR2U32(data + 4);
        cell.pressure = *(float*)(&temp);
        m_param->systemParam.pressureCalibTable << cell;
    }
        break;
    case PFC_PES_PULSECOMPEN_DATA_CMD:
        if (data[0])
        {
            m_param->systemParam.pulseCompenCalibTable.clear();
        }
        else
        {
            // 保存

            QVariant var;
            var.setValue(m_param->systemParam.pulseCompenCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetPulseCompenCalibData, var);
        }
        break;
    case PFC_PES_PULSECOMPEN_DATA:
    {
        hplc::PumpPulseCompenCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.position = *(float*)(&temp);

        if (m_param->systemParam.pulseCompenCalibTable.size())
        {
            if (cell.position == m_param->systemParam.pulseCompenCalibTable.last().position)
                break;
        }

        temp = PTR2U32(data + 4);
        cell.factor = *(float*)(&temp);
        m_param->systemParam.pulseCompenCalibTable << cell;
    }
        break;
    case PFC_PES_PULSECOMPEN_DEBUG_CMD:
        break;
    case PFC_PES_PULSECOMPEN_GEAR_POS:
        {
            qint32 pos = (int32_t)PTR2U32(data);

            QVariant var;
            var.setValue(pos);
            emit parseCompleted(hplc::PumpSystemCmdID_SetPulseCompenGearPos, var);
        }
        break;
    case PFC_PES_STEPMOTOR_IRUN:
    {
        m_param->systemParam.stepMotorIRun = data[0];

        QVariant var;
        var.setValue(m_param->systemParam.stepMotorIRun);
        emit parseCompleted(hplc::PumpSystemCmdID_SetStepMotorIRun, var);
    }
        break;
    case PFC_PES_MOTORCURRENT_DATA_CMD:
        if (data[0])
        {
            m_param->systemParam.stepMotorCurrentCalibTable.clear();
        }
        else
        {
            // 保存

            QVariant var;
            var.setValue(m_param->systemParam.stepMotorCurrentCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetMotorCurrentCalibData, var);
        }
        break;
    case PFC_PES_MOTORCURRENT_DATA:
    {
        hplc::PumpStepMotorCurrentCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.rpm = *(float*)(&temp);

        if (m_param->systemParam.stepMotorCurrentCalibTable.size())
        {
            if (qFuzzyCompare(cell.rpm, m_param->systemParam.stepMotorCurrentCalibTable.last().rpm))
                break;
        }

        cell.current = data[4];
        m_param->systemParam.stepMotorCurrentCalibTable << cell;
    }
        break;
    case PFC_PES_PUMPMATERIAL:
        {
            m_param->mechanicalParam.material =
                    static_cast<hplc::PumpMaterial>(data[0]);

            QVariant var;
            var.setValue(data[0]);
            emit parseCompleted(hplc::PumpSystemCmdID_SetPumpMaterial, var);
        }
        break;
    case PFC_PES_CURRENTRPM:
        {
            quint32 temp = PTR2U32(data);
            m_param->basicParam.currentRpm = *(float*)(&temp);

            QVariant var;
            var.setValue(m_param->basicParam.currentRpm);
            emit parseCompleted(hplc::PumpSystemCmdID_SetCurrentRpm, var);
        }
        break;
    default:
        break;
    }
}

void HplcZkysPumpParseSystemPacket::parseGet(quint8 pfc, char *data, int len)
{
    switch (pfc)
    {
    case PFC_PES_WORKMODE:
        break;
    case PFC_PES_FLOWCALIB_DATA_CMD:
        break;
    case PFC_PES_FLOWCALIB_DATA:
        break;
    case PFC_PES_FLOWCALIB_LOADPARAM:
        break;
    case PFC_PES_PRESSCALIB_DATA_CMD:
        break;
    case PFC_PES_PRESSCALIB_DATA:
        break;
    case PFC_PES_PULSECOMPEN_DATA_CMD:
        break;
    case PFC_PES_PULSECOMPEN_DATA:
        break;
    case PFC_PES_PULSECOMPEN_DEBUG_CMD:
        break;
    case PFC_PES_PULSECOMPEN_GEAR_POS:
        break;
    case PFC_PES_STEPMOTOR_IRUN:
        break;
    case PFC_PES_MOTORCURRENT_DATA_CMD:
        break;
    case PFC_PES_MOTORCURRENT_DATA:
        break;
    case PFC_PES_PUMPMATERIAL:
        break;
    case PFC_PES_CURRENTRPM:
        break;
    default:
        break;
    }
}

} // zkys
} // hplc
